/**
 * @author yxw
 * date : 2013年11月18日 下午7:28:00 
 */
package com.tzinfo.tzt.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.tzinfo.tzt.R;
import com.tzinfo.tzt.activity.util.MyApplication;
import com.tzinfo.tzt.activity.view.ImageAdapter;
import com.tzinfo.tzt.domain.NewhouseList;
import com.tzinfo.tzt.domain.OldhouseList;
import com.tzinfo.tzt.domain.RenthouseList;
import com.tzinfo.tzt.service.HouseHistoryService;

public class MyHistoryActivity extends BaseActivity implements OnClickListener,
		OnItemClickListener, OnItemLongClickListener {

	// 公共部分
	private RadioGroup radioGroup;
	private MyApplication mApp;
	private Button backBtn;
	private int tabNumCollection = 0;
	private LinearLayout cleanLayout;
	private ListView listView;
	private ImageAdapter imageAdapter;
	private RelativeLayout noCllectionLayout, cllectionListLayout;
	private HouseHistoryService historyService;

	private List<Map<String, Object>> oldHouseData;// 二手房数据list
	private List<Map<String, Object>> newHouseData;// 新房数据list
	private List<Map<String, Object>> rentHouseData;// 租房数据list

	//标志
	public String sHouseId;
	
	static String sSize = "size";
	static String sPrice = "price";
	// =================二手房相关===============================
	static String sID = "sID";
	static String sTime = "Time";
	static String sStyle = "Style";
	static String sLogo = "Logo";
	static String sName = "Name";
	static String sAddress = "Address";
	// END =================新房相关===============================
	// =================新房相关===============================
	// UI组件
	static String sAttr = "attr";

	// END =================新房相关===============================

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_history);
		init();
		historyService = new HouseHistoryService(this);
		getOldHouseData();
	}

	private void init() {
		mApp = (MyApplication) getApplication();
		mApp.setExit(false);
		backBtn = (Button) this.findViewById(R.id.bt_back);
		backBtn.setOnClickListener(this);

		radioGroup = (RadioGroup) findViewById(R.id.rgroup_under_top);
		radioGroup.setOnCheckedChangeListener(checkedChangeListener);

		listView = (ListView) this.findViewById(R.id.listView);
		listView.setOnItemClickListener(this);
		listView.setOnItemLongClickListener(this);
		noCllectionLayout = (RelativeLayout) this
				.findViewById(R.id.layout_no_collection);
		cllectionListLayout = (RelativeLayout) this
				.findViewById(R.id.layout_collection_list);
		cleanLayout = (LinearLayout) this.getLayoutInflater().inflate(
				R.layout.item_clean_bar, null);
		cleanLayout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {
				// 根据tabNumCollection清楚记录
				if (cleanDataByTab(tabNumCollection)) {
					switch (tabNumCollection) {
					case 0:
						oldHouseData.clear();
						imageAdapter.notifyDataSetChanged();
						break;
					case 1:
						newHouseData.clear();
						imageAdapter.notifyDataSetChanged();
						break;
					case 2:
						rentHouseData.clear();
						imageAdapter.notifyDataSetChanged();
						break;
					default:
						break;
					}
				}
				noCllectionLayout.setVisibility(View.VISIBLE);
				cllectionListLayout.setVisibility(View.GONE);
			}
		});
		listView.addFooterView(cleanLayout);

	}

	private void getOldHouseData() {
		List<OldhouseList> list = historyService.getOldHouseList();
		oldHouseData = new ArrayList<Map<String, Object>>();
		if (list != null && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				Map<String, Object> map = new HashMap<String, Object>();
				OldhouseList oldHouse = list.get(i);
				map.put(sLogo, oldHouse.getLogo());
				String strName = oldHouse.getName();
				if (strName.length() > 9) {
					strName = strName.substring(0, 9) + "…";
				}
				map.put(sName, strName);
				String strAddress = oldHouse.getAddress();
				if (strAddress.length() > 20) {
					strAddress = strAddress.substring(0, 20) + "…";
				}
				map.put(sAddress, strAddress);
				map.put(sSize, oldHouse.getStyle() + " " + oldHouse.getSize()
						+ "平米");
				String price = oldHouse.getPrice();
				if ("0万".equals(price)) {
					price = "售价待定";
				}
				map.put(sPrice, price);
				map.put(sID, oldHouse.getID());
				map.put(sTime, oldHouse.getTime());
				oldHouseData.add(map);
			}
			imageAdapter = new ImageAdapter(this, oldHouseData,
					R.layout.item_history_list, new String[] { sLogo, sName,
							sAddress, sSize, sPrice, sID, sTime }, new int[] {
							R.id.list_item_image, R.id.list_item_title,
							R.id.list_item_address, R.id.list_item_size,
							R.id.list_item_price, R.id.un_house_id,
							R.id.list_item_time });
			listView.setAdapter(imageAdapter);
			noCllectionLayout.setVisibility(View.GONE);
			cllectionListLayout.setVisibility(View.VISIBLE);
		} else {
			noCllectionLayout.setVisibility(View.VISIBLE);
			cllectionListLayout.setVisibility(View.GONE);
		}
	}

	private void getNewHouseData() {
		List<NewhouseList> list = historyService.getNewHouseList();
		newHouseData = new ArrayList<Map<String, Object>>();
		if (list != null && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				Map<String, Object> map = new HashMap<String, Object>();
				NewhouseList newHouse = list.get(i);
				map.put(sLogo, newHouse.getLogo());
				String strName = newHouse.getName();
				if (strName.length() > 8) {
					strName = strName.substring(0, 8) + "…";
				}
				map.put(sName, strName);
				String strAdress = newHouse.getAddress();
				if (strAdress.length() > 20) {
					strAdress = strAdress.substring(0, 20) + "…";
				}
				map.put(sAddress, strAdress);
				map.put(sSize, newHouse.getStatus());
				String price = newHouse.getPrice() + "";
				if (newHouse.getPrice() == 0) {
					price = "售价待定";
				} else {
					price += "元/" + newHouse.getPriceUnit();
				}
				map.put(sPrice, price);
				int attrId = R.drawable.transparent;
				int attrType;
				switch (attrType = newHouse.getAttr()) {
				case 1:
					attrId = R.drawable.tuan;
					break;
				case 2:
					attrId = R.drawable.kan;
					break;
				case 3:
					attrId = R.drawable.fang;
					break;
				}
				map.put(sAttr, attrId);
				map.put(sID, newHouse.getId() + "&&" + attrType);
				map.put(sTime, newHouse.getTime());
				newHouseData.add(map);
			}
			imageAdapter = new ImageAdapter(this, newHouseData, R.layout.item_history_list,
					new String[] { sLogo, sName, sAddress, sSize, sPrice, sAttr,
							sID, sTime }, new int[] { R.id.list_item_image,
							R.id.list_item_title, R.id.list_item_address,
							R.id.list_item_size, R.id.list_item_price,
							R.id.img_attr, R.id.un_house_id, R.id.list_item_time });
			listView.setAdapter(imageAdapter);
			noCllectionLayout.setVisibility(View.GONE);
			cllectionListLayout.setVisibility(View.VISIBLE);
		} else {
			noCllectionLayout.setVisibility(View.VISIBLE);
			cllectionListLayout.setVisibility(View.GONE);
		}
	}

	private void getRentHouseData() {
		List<RenthouseList> list = historyService.getRentHouseList();
		rentHouseData = new ArrayList<Map<String, Object>>();
		if (list != null && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				Map<String, Object> map = new HashMap<String, Object>();
				RenthouseList rentHouse = list.get(i);
				map.put(sLogo, rentHouse.getLogo());
				String strName = rentHouse.getName();
				if (strName.length() > 9) {
					strName = strName.substring(0, 9) + "…";
				}
				map.put(sName, strName);
				String strAddress = rentHouse.getAddress();
				if (strAddress.length() > 20) {
					strAddress = strAddress.substring(0, 20) + "…";
				}
				map.put(sAddress, strAddress);
				map.put(sSize, rentHouse.getStyle());
				String price = rentHouse.getPrice();
				if ("0元".equals(price)) {
					price = "售价待定";
				} else {
					price += "/" + rentHouse.getPriceUnit();
				}
				map.put(sPrice, price);
				map.put(sID, rentHouse.getID());
				map.put(sTime, rentHouse.getTime());
				rentHouseData.add(map);
			}
			imageAdapter = new ImageAdapter(this, rentHouseData,
					R.layout.item_history_list, new String[] { sLogo, sName,
							sAddress, sSize, sPrice, sID, sTime }, new int[] {
							R.id.list_item_image, R.id.list_item_title,
							R.id.list_item_address, R.id.list_item_size,
							R.id.list_item_price, R.id.un_house_id,
							R.id.list_item_time });
			listView.setAdapter(imageAdapter);
			noCllectionLayout.setVisibility(View.GONE);
			cllectionListLayout.setVisibility(View.VISIBLE);
		} else {
			noCllectionLayout.setVisibility(View.VISIBLE);
			cllectionListLayout.setVisibility(View.GONE);
		}
	}

	protected boolean cleanDataByTab(int tab) {
		boolean flag = historyService.deleteAll(tab);
		Toast.makeText(MyHistoryActivity.this, "数据已清空！", 5000).show();
		return flag;
	}

	protected boolean cleanDataById(String id) {
		boolean flag=false;
		switch (tabNumCollection) {
		case 0:
			flag = historyService.deleteOldById(id);
			getOldHouseData();
			break;
		case 1:
			String[] ids=id.split("&&");
			flag = historyService.deleteNewById(ids[0]);
			getNewHouseData();
			break;
		case 2:
			flag = historyService.deleteRentById(id);
			getRentHouseData();
			break;
		default:
			flag = false;
			break;
		}
		return flag;
	}

	private OnCheckedChangeListener checkedChangeListener = new OnCheckedChangeListener() {
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			tabNumCollection = checkedId - R.id.radio_top_btn1;
			switch (checkedId) {
			case R.id.radio_top_btn1:
				getOldHouseData();
				break;
			case R.id.radio_top_btn2:
				getNewHouseData();
				break;
			case R.id.radio_top_btn3:
				getRentHouseData();
				break;
			}
		}
	};

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		switch (tabNumCollection) {
		case 0:
			getOldHouseData();
			break;
		case 1:
			getNewHouseData();
			break;
		case 2:
			getRentHouseData();
			break;
		}
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.bt_back:
			this.finish();
			break;
		default:
			break;
		}

	}

	@Override
	public void onItemClick(AdapterView<?> adapterview, View view, int i, long l) {
		TextView textView;
		String houseId;
		switch (tabNumCollection) {
		case 0:
			Intent intent = new Intent(this, OldHouseDetailActivity.class);
			textView = (TextView) view.findViewById(R.id.un_house_id);
			houseId = textView.getText().toString();
			OldhouseList item = historyService.getOldHouseById(houseId);
			item.setID(Integer.parseInt(houseId));
			intent.putExtra("houseId", houseId);
			intent.putExtra("oldhouseList", item);
			historyService.saveOldHouse(item);
			startActivity(intent);
			break;
		case 1:
			Intent intent1 = new Intent(this, NewHouseDetailActivity.class);
			textView = (TextView) view.findViewById(R.id.un_house_id);
			houseId = textView.getText().toString().trim();
			String[] strParas = houseId.split("&&");
			String shouseId=houseId;
			houseId = strParas[0];
			NewhouseList item1 = historyService.getNewHouseById(houseId);
			item1.setId(Integer.parseInt(houseId));
			intent1.putExtra("houseId", shouseId);
			intent1.putExtra("newhouseList", item1);
			historyService.saveNewHouse(item1);
			startActivity(intent1);
			break;
		case 2:
			Intent intent2 = new Intent(this, RentHouseDetailActivity.class);
			textView = (TextView) view.findViewById(R.id.un_house_id);
			houseId = textView.getText().toString();
			RenthouseList item2 = historyService.getRentHouseById(houseId);
			item2.setID(Integer.parseInt(houseId));
			intent2.putExtra("houseId", houseId);
			intent2.putExtra("renthouseList", item2);
			historyService.saveRentHouse(item2);
			startActivity(intent2);
			break;
		default:
			break;
		}
	}

	@Override
	public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		TextView textView=(TextView) arg1.findViewById(R.id.un_house_id);
		sHouseId=textView.getText().toString();
		new AlertDialog.Builder(this).setCancelable(false)
				.setMessage("您确认要删除该记录吗？")
				.setPositiveButton(getResources().getString

				(R.string.cancel), new DialogInterface.OnClickListener()

				{
					public void onClick

					(DialogInterface dialog, int which) {
						//取消
					}
				}).setNegativeButton(getResources().getString

				(R.string.sure), new DialogInterface.OnClickListener()

				{
					public void onClick(

					DialogInterface dialogInterface, int

					whichButton) {
						//确定
						cleanDataById(sHouseId);
					}
				}).show();
		return false;
	}
}
